﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.ServiceModel.Activation;
using System.ServiceModel.Syndication;
using PBDesk.Business.FeedsAggregator;
using PBDesk.Utilities;


using PBDesk.Framework.LogNExpMgmt;

namespace PBDesk.FeedService
{
    [AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
    public class PBDeskFeed : IPBDeskFeed
    {


        public PBDeskFeed()
        {

            if (HttpContext.Current.Request.Url.AbsolutePath.EndsWith("/"))
                Log.Stat(HttpContext.Current.Request.Url.AbsoluteUri);
  
        }
       
        private const int DEFAULT_FEED_ITEM_COUNT = 50;
        public SyndicationFeedFormatter GetFeeds1(string categoryName)
        {
            return FeedManager.GetFeeds(categoryName, GetItemCount());            
        }

        public SyndicationFeedFormatter GetFeeds2(string categoryName, string subCategoryName)
        {
            return FeedManager.GetFeeds(categoryName, subCategoryName, GetItemCount());
        }

        public SyndicationFeedFormatter GetFeeds3(string categoryName, string subCategoryName, string z)
        {
            return FeedManager.GetFeeds(categoryName,subCategoryName,z);
        }

        public SyndicationFeedFormatter GetFeeds()
        {
            return FeedManager.GetFeeds("rss", GetItemCount());
        }


        public SyndicationFeedFormatter GetFeeds4(string categoryName, string subCategoryName, string z, string w)
        {
            return FeedManager.GetFeeds(categoryName,subCategoryName,z,w);
        }

        private int GetItemCount()
        {            
            string tempStr = HttpContext.Current.Request.QueryString["n"];
            int returnValue = DEFAULT_FEED_ITEM_COUNT;
            if (tempStr.IsNotNullAndNotEmpty())
            {
                int count;
                if (int.TryParse(tempStr, out count))
                {
                    if (count < 0) returnValue = int.MaxValue;
                    else returnValue = count;
                }
            }
            return returnValue;
        }

        private void WriteLog()
        {
            
        }
    }
}